package com.fsnbigdata.web;

import com.fsnbigdata.util.AliOSSUtil;
import com.fsnbigdata.vo.ImageVo;
import com.fsnbigdata.vo.VideoVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by zhongcheng on 19/09/2017.
 */

@Controller
@RequestMapping(value = "/api/videos", name = "VideoController", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "Videos", description = "视频" )
public class VideoController {

    private AliOSSUtil aliOSSUtil;

    @Autowired
    public VideoController(AliOSSUtil aliOSSUtil) {
        this.aliOSSUtil = aliOSSUtil;
    }

    @PostMapping(value = "/upload", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
    //@PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> upload(
            @RequestParam("file") MultipartFile file
            //@ModelAttribute("file") FormDataContentDisposition fileDisposition
    ) throws IOException {
        String url = aliOSSUtil.uploadVideo(file);

        VideoVo videoVo = new VideoVo();
        videoVo.setUrl(url);
        videoVo.setName(file.getOriginalFilename());
        videoVo.setContentType(file.getContentType());

        return ResponseEntity.ok(videoVo);
    }

}
